﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace chengz
{
    public partial class yunscl : Form
    {
        DataTable dt = null;
        OleDbConnection conn = null;

        public yunscl()
        {
            InitializeComponent();
        }

        private void yunscl_Load(object sender, EventArgs e)
        {
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + @"\chengzhongku.mdb";
            conn = new OleDbConnection(connectionString);
            conn.Open();

            string sql = "SELECT  id as ID, chepNu AS 车牌号, piz AS 皮重 FROM cz_yunscl";

            OleDbDataAdapter thisAdapter = new OleDbDataAdapter(sql, conn);
            System.Data.DataSet thisDataSet = new System.Data.DataSet();
            thisAdapter.Fill(thisDataSet, "table");
            dt = thisDataSet.Tables["table"];
            this.dataGridView1.DataSource = dt;
            //conn.Close();
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string strcolumn = dataGridView1.Columns[e.ColumnIndex].HeaderText;//获取列标题
            string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();//获取焦点触发行的第一个值
            string value = dataGridView1.CurrentCell.Value.ToString();//获取当前点击的活动单元格的值
            
            OleDbCommand cmd = conn.CreateCommand();
            if (strcolumn.Equals("车牌号"))
                cmd.CommandText = "update cz_yunscl set chepNu = '" + value + "' where id = " + id;

            if (strcolumn.Equals("皮重"))
                cmd.CommandText = "update cz_yunscl set piz = '" + value + "' where id = " + id;

            try
            {
                if (cmd.ExecuteNonQuery() > 0)
                    MessageBox.Show("操作成功", "提示");
                else
                    MessageBox.Show("操作失败", "提示");

            } catch (Exception error)
            {
                Console.WriteLine(error.Message);
            }
        }

        void search()
        {
            string sql = "SELECT  id as ID, chepNu AS 车牌号, piz AS 皮重 " +
                "FROM cz_yunscl where chepNu like '%" + textBox_chepNu.Text + "%'";

            OleDbDataAdapter thisAdapter = new OleDbDataAdapter(sql, conn);
            System.Data.DataSet thisDataSet = new System.Data.DataSet();
            thisAdapter.Fill(thisDataSet, "table");
            dt = thisDataSet.Tables["table"];
            this.dataGridView1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            search();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DialogResult dr = MessageBox.Show(this, "确认删除么？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
                    string id = drv.Row["ID"] + "";

                    OleDbCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "delete from cz_yunscl where id = " + id;

                    try
                    {
                        if (cmd.ExecuteNonQuery() > 0)
                        {
                            MessageBox.Show("操作成功", "提示");
                            drv.Row.Delete();
                        }
                        else
                            MessageBox.Show("操作失败", "提示");

                    }
                    catch (Exception error)
                    {
                        MessageBox.Show(error.Message, "提示");
                    }
                }
                
            } else
                MessageBox.Show("请选择要删除的记录！", "提示");
        }

        private void textBox_chepNu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                search();
        }
    }
}
